package com.woniu.sharepark.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.woniu.sharepark.realm.CustomRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @author SeaMonster
 * @date 2019/6/3 20:04
 * version v1.0
 */
@Configuration
public class ShiroConfig {


    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){

        ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager());



        Map<String, String> map = new HashMap<String, String>(16);
        // 登出
        map.put("/login.html","anon");

        map.put("/pc/**","anon");
        map.put("/mobile/**","anon");
        map.put("/","anon");
        // 对所有用户认证
        map.put("/user/*", "anon");
        map.put("/index", "anon");
        map.put("/mlogin.html", "anon");
        map.put("/mregister.html", "anon");
        map.put("/carIndex", "anon");
        map.put("/community/mapList", "anon");
        map.put("/lessor/findParkByComId", "anon");
        map.put("/alipay/alipayNotifyNotice","anon");
        map.put("/**","authc");

        shiroFilterFactoryBean.setSuccessUrl("/index");
        shiroFilterFactoryBean.setLoginUrl("/login.html");


        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        return shiroFilterFactoryBean;
    }


    /**
     * 配置Shiro过滤器。指定哪些资源需要进行认证后才能访问。
     * @return
     */
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){
        DefaultShiroFilterChainDefinition defaultShiroFilterChainDefinition = new DefaultShiroFilterChainDefinition();

        return defaultShiroFilterChainDefinition;
    }



    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(){
        DefaultWebSecurityManager defaultWebSecurityManager=new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(customRealm());
        return defaultWebSecurityManager;

    }
    /**
     * 创建自定义域对象，注入到IOC容器中
     * @return
     */
    @Bean(value="authorizer")
    public CustomRealm customRealm(){
        CustomRealm customRealm = new CustomRealm();
        customRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        customRealm.setCacheManager(cacheManager());
        return customRealm;
    }


    /**
     * 创建密码匹配器对象
     * @return
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");
        matcher.setHashIterations(2);
        return matcher;
    }



    /**
     * 缓存管理器对象
     * @return
     */
    @Bean(name = "cacheshiro")
    public MemoryConstrainedCacheManager cacheManager(){
        return new MemoryConstrainedCacheManager();
    }


    /**
     * thymeleaf中使用shiro标签
     * @return
     */
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }

    @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(defaultWebSecurityManager());
    return authorizationAttributeSourceAdvisor;
    }

}